/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mini_game_fx;

import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * This is a JavaFX version of Mini game Framework KeyEventRelayer
 * class.
 *
 * The original MiniGameEventRelayerFx class implements MouseListener,
 * MouseDragListener, and KeyListener. Instead, JavaFX do override the
 * handle() method to control each of events.
 *
 * @author Heecheon Kim
 * @author Taewan Kim
 * @author Jaewoong Shin
 */
public class MiniGameKeyEventRelayerFx implements EventHandler<KeyEvent>{

    // THE GAME FROM WHICH THE EVENTS WERE GENERATED
    private MiniGameFx game;
    
    /**
     * The constructor just sets up access to the game for which to respond
     * to.
     * 
     * @param initGameFx the game being played.
     */
    public MiniGameKeyEventRelayerFx(MiniGameFx initGameFx) {
        game = initGameFx;
    }
    @Override
    public void handle(KeyEvent ke) {
        if(ke.getEventType() == KeyEvent.KEY_PRESSED) {
            keyPressed(ke);
        }
        else if(ke.getEventType() == KeyEvent.KEY_RELEASED) {
            keyReleased(ke);
        }
        else if(ke.getEventType() == KeyEvent.KEY_TYPED) {
            keyTyped(ke);
        }
    }
    
    /**
     * This key handler simply activates our debug test display, which
     * may then be rendered by the canvas.
     * 
     * @param ke Key of the pressed.
     */
    public void keyPressed(KeyEvent ke) {
        if(ke.getCode() == KeyCode.D) {
            System.out.println("DDD");
            // TOGGLE IT OFF
            if (game.getDataModel().isDebugTextRenderingActive()) {
                game.getDataModel().deactivateDebugTextRendering();
            }
            // TOGGLE IT OFF
            else {
                game.getDataModel().activateDebugTextRendering();
            }
            
            // Consumes this event so that it will not be processed in the
            // default manner by the source which originated it.
            ke.consume();
        }
        else if(ke.getCode() == KeyCode.P) {
            System.out.println("ppp");
            // TOGGLE IT OFF
            if(game.getDataModel().isPaused()) {
                game.getDataModel().unpause();
            }
            // TOGGLE IT ON
            else {
                game.getDataModel().pause();
            }
            
            ke.consume();
        }
    }
    
    /**
     * WE WILL NOT USE THESE METHOD.
     * 
     * @param ke Key event associated with key released.
     */
    public void keyReleased(KeyEvent ke) {
        
    }
    
    /**
     * WE WILL NOT USE THESE METHOD.
     * 
     * @param ke Key event associated with key typed.
     */
    public void keyTyped(KeyEvent ke) {
        
    }
}
